package edu.rit.varapp.datalayer;

import java.util.LinkedHashMap;
import java.util.ArrayList;
import java.util.HashMap;

import static edu.rit.varapp.utilities.StringUtils.*;

/**
 * Represents the customer table in the database.
 */
public class Customer {

    private int EntityId;
    private String ContactName;
    private String CompanyName;
    private String Title;
    private String Addr1;
    private String Addr2;
    private String City;
    private String State;
    private String Zipcode;
    private String Email;

    /**
     * Creates a Customer object using a collection of strings. The list must contain the strings in the correct order
     * as the full constructor.
     */
    public Customer( ArrayList<String> data ) {
        this.EntityId = Integer.valueOf( data.get( 0 ) );
        this.Title = data.get( 1 );
        this.ContactName = data.get( 2 );
        this.CompanyName = data.get( 3 );
        this.Addr1 = data.get( 4 );
        this.Addr2 = data.get( 5 );
        this.City = data.get( 6 );
        this.State = data.get( 7 );
        this.Zipcode = data.get( 8 );
        this.Email = data.get( 9 );
    }

    /**
     * Full constructor.
     */
    public Customer( int EntityId, String Title, String ContactName, String CompanyName, String Addr1, String Addr2,
                     String City, String State, String Zip, String Email ) {
        this.EntityId = EntityId;
        this.Title = Title;
        this.ContactName = ContactName;
        this.CompanyName = CompanyName;
        this.Addr1 = Addr1;
        this.Addr2 = Addr2;
        this.City = City;
        this.State = State;
        this.Zipcode = Zip;
        this.Email = Email;
    }

    public String getAddr1() {
        return Addr1;
    }

    public String getAddr2() {
        return Addr2;
    }

    public String getCity() {
        return City;
    }

    public String getCompanyName() {
        return CompanyName;
    }

    public String getContactName() {
        return ContactName;
    }

    public String getEmail() {
        return Email;
    }

    public int getEntityId() {
        return EntityId;
    }

    public String getState() {
        return State;
    }

    public String getZipcode() {
        return Zipcode;
    }

    public String getTitle() {
        return Title;
    }

    @Override
    public String toString() {
        return "Customer{ EntityId=" + EntityId
               + " Title= " + Title
               + " ContactName=" + ContactName
               + " CompanyName=" + CompanyName
               + " Addr1=" + Addr1
               + " Addr2=" + Addr2
               + " City=" + City
               + " State=" + State
               + " Zipcode=" + Zipcode
               + " Email=" + Email + " }";
    }

    public static String columnsToSQL() {
        return "EntityId, Title, ContactName, CompanyName, Addr1, Addr2, City, State, Zip, Email";
    }

    /**
     * Returns the values of this object with or without single quotes.
     * @param sqlCall Flag that says if we want to wrap strings with single quotes (for SQL queries). Set flag to true
     *                if we want single quotes, else false.
     * @return
     */
    public String valuesToSQL( boolean sqlCall ) {
        if( sqlCall ) {
            return (EntityId + ", '" + Title + "', '" + ContactName + "', '" + CompanyName + "', '"
                    + Addr1 + "', '" + Addr2 + "', '" + City + "', '" + State + "', '"
                    + Zipcode + "', '" + Email + "'").trim();
        } else {
            return (EntityId + ", " + Title + ", " + ContactName + ", " + CompanyName + ", "
                    + Addr1 + ", " + Addr2 + ", " + City + ", " + State + ", "
                    + Zipcode + ", " + Email).trim();
        }
    }

    /**
     * Returns this Customer object as a hashmap with keys being the field name, and values being the field's
     * current value.
     * @param includeEntityId Flag to say if we want to return the entityId in the map. Since the entityId is not
     * needed when viewing a customer's information, it might not be needed in the HashMap. Set to true if the entityId
     * should be returned within the HashMap.
     */
    public HashMap<String, String> asMap( boolean includeEntityId ) {
        HashMap<String, String> ret = new LinkedHashMap<String, String>();
        if( includeEntityId ) ret.put( "EntityId", String.valueOf( EntityId ) );
        ret.put( "Title", Title );
        ret.put( "ContactName", ContactName );
        ret.put( "CompanyName", CompanyName );
        if( !checkNull( Email ) ) ret.put( "Email", Email );
        else ret.put( "Email", "" );
        if( !checkNull( Addr1 ) ) ret.put( "Addr1", Addr1 );
        else ret.put( "Addr1", "" );
        if( !checkNull( Addr2 ) ) ret.put( "Addr2", Addr2 );
        else ret.put( "Addr2", "" );
        if( !checkNull( City ) ) ret.put( "City", City );
        else ret.put( "City", "" );
        if( !checkNull( State ) ) ret.put( "State", State );
        else ret.put( "State", "" );
        if( !checkNull( Zipcode ) ) ret.put( "Zip", Zipcode );
        else ret.put( "Zip", "" );
        return ret;
    }

    /**
     * Nicely-formatted keys map.
     * @see Customer#asMap(boolean) 
     */
    public HashMap<String, String> niceMap( boolean includeEntityId ) {
        HashMap<String, String> ret = new LinkedHashMap<String, String>();
        if( includeEntityId ) ret.put( "EntityId", String.valueOf( EntityId ) );
        ret.put( "Title", Title );
        ret.put( "Contact Name", ContactName );
        if( !checkNull( Email ) ) ret.put( "Email", Email );
        else ret.put( "Email", "" );
        ret.put( "Company Name", CompanyName );
        if( !checkNull( Addr1 ) ) ret.put( "Addr1", Addr1 );
        else ret.put( "Address 1", "" );
        if( !checkNull( Addr2 ) ) ret.put( "Addr2", Addr2 );
        else ret.put( "Address 2", "" );
        if( !checkNull( City ) ) ret.put( "City", City );
        else ret.put( "City", "" );
        if( !checkNull( State ) ) ret.put( "State", State );
        else ret.put( "State", "" );
        if( !checkNull( Zipcode ) ) ret.put( "Zipcode", Zipcode );
        else ret.put( "Zipcode", "" );
        return ret;
    }
}
